// pages/my/my.js
Page({
  data: {
    isHidden: true,
    userInfo: null,
    nickName: '',
    avatarUrl: ''
  },

  onShow() {
    var userInfo = wx.getStorageSync('userInfo')
    if (userInfo && userInfo.nickName && userInfo.avatarUrl) {
      this.setData({
        userInfo
      })
    }
  },
  getAvatar(e) {
    this.setData({
      avatarUrl: e.detail.avatarUrl
    })
  },
  getName(e) {
    this.setData({
      nickName: e.detail.value
    })
  },
  goLogin() {
    this.setData({
      isHidden: false,
      // 重置表单
      nickName: '',
      avatarUrl: ''
    })
  },
  logout() {
    wx.showModal({
      title: '提示',
      content: '确定要退出登录吗？',
      success: (res) => {
        if (res.confirm) {
          this.setData({
            userInfo: null
          })
          wx.setStorageSync('userInfo', null)
          wx.showToast({
            title: '已退出登录',
            icon: 'none'
          })
        }
      }
    })
  },

  popNO() {
    this.setData({
      isHidden: true
    })
  },

  popYes() {
    var avatarUrl = this.data.avatarUrl
    var nickName = this.data.nickName

    if (!avatarUrl) {
      return wx.showToast({
        icon: 'error',
        title: '请获取头像',
      })
    }
    if (!nickName) {
      return wx.showToast({
        icon: 'error',
        title: '请输入昵称',
      })
    }

    wx.showLoading({
      title: '登录中...',
    })

    wx.cloud.callFunction({
      name: 'getMyOpenId',
      success: openIdRes => {
        wx.cloud.database().collection('userInfo')
          .where({
            _openid: openIdRes.result.openid,
          })
          .get().then(getUserInfoRes => {
            wx.cloud.uploadFile({
              cloudPath: nickName + '.png',
              filePath: avatarUrl,
            }).then(uploadRes => {
              var userInfo = {
                avatarUrl: uploadRes.fileID,
                nickName
              }
              wx.setStorageSync('userInfo', userInfo)
              if (getUserInfoRes.data && getUserInfoRes.data.length > 0) {
                wx.cloud.database().collection('userInfo')
                  .where({
                    _openid: openIdRes.result.openid,
                  }).update({
                    data: userInfo
                  }).then(updataUserInfoRes => {
                    this.setData({
                      userInfo,
                      isHidden: true
                    })
                    wx.hideLoading()
                  }).catch(updataUserInfoRes => {
                    console.log(updataUserInfoRes)
                  })
              } else {
                wx.cloud.database().collection('userInfo')
                  .add({
                    data: userInfo
                  }).then(addUserInfoRes => {
                    this.setData({
                      userInfo,
                      isHidden: true
                    })
                    wx.hideLoading()
                  }).catch(addUserInfoRes => {
                    console.log(addUserInfoRes)
                  })
              }
            }).catch(uploadRes => {
              console.log(uploadRes)
            })
          }).catch(getUserInfoRes => {
            console.log(getUserInfoRes)
          })
      },
      fail(err) {
        console.log('云函数请求失败', err)
      }
    })
  },
  collect() {
    wx.navigateTo({
      url: '/pages/collect/collect',
    })
  },
  publish() {
    wx.navigateTo({
      url: '/pages/demo1/demo1',
    })
  }
})